﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Web;
using log4net;
using log4net.Layout;
using log4net.Layout.Pattern;

namespace App.MVC.Extend
{
    /// <summary>
    /// 包含了所有的自定字段属性
    /// </summary>
    public  class LogHelper
    {
        private LogHelper()
        {
            //SetConfig();
        }

        public static readonly log4net.ILog logsqlinfo = log4net.LogManager.GetLogger("logsqlinfo");

        public static void SetConfig()
        {
            log4net.Config.XmlConfigurator.Configure();
        }

        public static void SetConfig(FileInfo configFile)
        {
            log4net.Config.XmlConfigurator.Configure(configFile);
        }

        /// <summary>
        /// //获取执行的位置
        /// </summary>
        /// <param name="frameindex"></param>
        /// <returns></returns>
        private static string GetDiaoYongZheMethod(int frameindex)
        {
            const int level = 1000;

            StackTrace ss = new StackTrace(true);
            //index:0为本身的方法；1为调用方法；2为其上上层，依次类推
            MethodBase mb = ss.GetFrame(frameindex).GetMethod();

            //本文地址:http://www.cnblogs.com/Interkey/p/GetMethodName.html
            StackFrame[] sfs = ss.GetFrames();
            string systemModule = Environment.NewLine;
            systemModule += "模块名:" + mb.Module.ToString() + Environment.NewLine;
            systemModule += "命名空间名:" + mb.DeclaringType.Namespace + Environment.NewLine;
            //仅有类名
            systemModule += "类名:" + mb.DeclaringType.Name + Environment.NewLine;
            systemModule += "方法名:" + mb.Name + Environment.NewLine;
            return systemModule;
        }
    
        /// <summary>
        /// 记录一般信息
        /// </summary>
        /// <param name="info"></param>
        public static void WriteLog_Sql(string info)
        {
           
            var systemModule = GetDiaoYongZheMethod(2);//获取执行的位置
            if (logsqlinfo.IsErrorEnabled)
            {
                logsqlinfo.Info(new LogContent(GetIP(), "111111", systemModule, info));
            }
        }
        /// <summary>
        /// 记录错误信息
        /// </summary>
        /// <param name="info"></param>
        /// <param name="ex"></param>
        public static void WriteLog_Sql(Exception ex)
        {
            if (logsqlinfo.IsErrorEnabled)
            {
                logsqlinfo.Error(new LogContent(GetIP(), "111111", ex.StackTrace, ex.ToString()));
            }
        }
        /// <summary>
        /// 获取IP
        /// </summary>
        /// <returns></returns>
        private static string GetIP()
        {
            string ip = string.Empty;
            if (!string.IsNullOrEmpty(System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"]))
                ip = Convert.ToString(System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]);
            if (string.IsNullOrEmpty(ip))
                ip = Convert.ToString(System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]);
            return ip;
        }
    }
}